package main.java.ensao.cryptography.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigInteger;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import main.java.ensao.cryptography.rsaattack.ModuleCommun;
import main.java.ensao.cryptography.rsamodule.Utils;

public class RSAattaquePane extends JPanel {

	/**
	 * 
	 */
  
	JTextField cA, cB, nA, eA, nB, eB , pgcd , U , V , m;


	JButton pgcd_label , bezout, calc_m;
	
	int result_m;
	
	private static final long serialVersionUID = 1L;

	public RSAattaquePane() {
		this.init();
	}

	public void init() {

		JPanel global = new JPanel();
		global.setLayout(new BoxLayout(global, BoxLayout.LINE_AXIS));

		JPanel alice = new JPanel();
		alice.setBorder(BorderFactory.createTitledBorder("Alice"));		
		alice.setPreferredSize(new Dimension(500,200));

		JPanel label_pan = new JPanel();
		label_pan.setLayout(new BoxLayout(label_pan, BoxLayout.Y_AXIS));

		JLabel c0 = new JLabel(
				"Alice envoie à Bob et Bernard le chiffré du même message : m ");


		JLabel chaine_envoi = new JLabel("Envoie  CA =");
		cA = new JTextField(2);
		cA.setText("12");
		cA.setEditable(false);
	

		JLabel chaine_envoii = new JLabel("Envoie  CB =");
		cB = new JTextField(2);
		cB.setText("29");
		cB.setEditable(false);

		label_pan.add(c0);

		
		JPanel grid = new JPanel();
		grid.setLayout(new GridLayout(3, 4, 1, 1));
		grid.add(new JLabel("     "));
		grid.add(new JLabel("  "));
		grid.add(new JLabel(" "));grid.add(new JLabel(" "));
		
		grid.add(chaine_envoi);
		grid.add(cA);
		grid.add(new JLabel(" à Bob"));grid.add(new JLabel(" "));
		grid.add(chaine_envoii);
		grid.add(cB);
		grid.add(new JLabel(" à Bernard"));grid.add(new JLabel(" "));
		label_pan.add(grid);
		
		alice.add(label_pan);

		JPanel Bob = new JPanel();
		Bob.setBorder(BorderFactory.createTitledBorder("Bob"));

		JLabel n = new JLabel("n =   ");

		nA = new JTextField(3);
		nA.setText("33");
		nA.setEditable(false);

		JPanel ligne_nA = new JPanel();
		ligne_nA.setLayout(new BoxLayout(ligne_nA, BoxLayout.LINE_AXIS));

		ligne_nA.add(n);
		ligne_nA.add(nA);

		JLabel e = new JLabel("e =   ");

		eA = new JTextField(3);
		eA.setText("3");
		eA.setEditable(false);

		JPanel ligne_eA = new JPanel();
		ligne_eA.setLayout(new BoxLayout(ligne_eA, BoxLayout.LINE_AXIS));

		ligne_eA.add(e);
		ligne_eA.add(eA);

		Bob.add(ligne_nA);
		Bob.add(ligne_eA);

		JPanel Bernard = new JPanel();
		Bernard.setBorder(BorderFactory.createTitledBorder("Bernard"));
		
		
		JLabel nb = new JLabel("n =   ");

		nB = new JTextField(3);
		nB.setText("33");
		nB.setEditable(false);

		JPanel ligne_nB = new JPanel();
		ligne_nB.setLayout(new BoxLayout(ligne_nB, BoxLayout.LINE_AXIS));

		ligne_nB.add(nb);
		ligne_nB.add(nB);

		JLabel eb = new JLabel("e =   ");

		eB = new JTextField(3);
		eB.setText("5");
		eB.setEditable(false);

		JPanel ligne_eB = new JPanel();
		ligne_eB.setLayout(new BoxLayout(ligne_eB, BoxLayout.LINE_AXIS));

		ligne_eB.add(eb);
		ligne_eB.add(eB);

		Bernard.add(ligne_nB);
		Bernard.add(ligne_eB);

		JPanel p2 = new JPanel();
		p2.setLayout(new BoxLayout(p2, BoxLayout.PAGE_AXIS));

		p2.add(Bob);
		p2.add(Bernard);

		
		JPanel oscar = new JPanel();
		oscar.setPreferredSize(new Dimension(730,250));
		
		oscar.setBorder(BorderFactory.createTitledBorder("Oscar"));
		
		JLabel label = new JLabel("Oscar intercepte CA , CB , n , eA, eB  et peut donc calculer m puisqu'il s'agit ");
		JLabel label2 = new JLabel("de deux chiffrés du même message. ");
		
		
		pgcd_label = new JButton("eA ^ eB =");
		pgcd = new JTextField(4);
		
		oscar.add(label, BorderLayout.EAST);
		oscar.add(label2);
		oscar.add(new JLabel("                                                        "));
		
		
		JPanel pgcd_bezout = new JPanel();
		pgcd_bezout.setBorder(BorderFactory.createTitledBorder("PGCD(eA,eB) & Identité de Bezout"));
		
		
		bezout = new JButton("eA.U + eB.V = 1");
		JLabel val_u = new JLabel("U :");
		U = new JTextField(3);
		JLabel val_v = new JLabel("V :");
		V = new JTextField(3);
		
		
		pgcd_bezout.add(pgcd_label);
		pgcd_bezout.add(pgcd);
		pgcd_bezout.add(bezout);
		pgcd_bezout.add(val_u);
		pgcd_bezout.add(U);
		pgcd_bezout.add(val_v);
		pgcd_bezout.add(V);
		
		
		JPanel calcul_m = new JPanel();
		calcul_m.setBorder(BorderFactory.createTitledBorder("Calcul de m"));
		
		calc_m = new JButton("calculer m  ");
		 m = new JTextField(10);
		
		 calcul_m.add(calc_m);
		 calcul_m.add(m);
		 
		 
		 
		
		oscar.add(pgcd_bezout, BorderLayout.EAST);
		oscar.add(calcul_m, BorderLayout.EAST);
		
		
		global.add(alice);
		JPanel separateur = new JPanel();
		separateur.add(new JLabel("                      "));
		global.add(separateur);
		global.add(p2);

		
		//traitement
		
		pgcd_label.addActionListener(new RSAattaque());
		bezout.addActionListener(new RSAattaque());
		calc_m.addActionListener(new RSAattaque());
		
		Font f = new Font("Serif", Font.PLAIN, 25);
		JLabel attaque = new JLabel("Attaque par module commun");
		attaque.setFont(f); 
		
		
		
		this.add(new JPanel().add(attaque));
		
		
		this.add(global);
		this.add(oscar);

	}
	
	BigInteger pgcd_listener = new BigInteger("0");
	class RSAattaque implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			
			if(e.getSource() == pgcd_label)
			{
				
				pgcd_listener = Utils.calcul_pgcd(BigInteger.valueOf(Long.parseLong(eA.getText())), BigInteger.valueOf(Long.parseLong(eB.getText())));
				
				pgcd.setText(String.valueOf(pgcd_listener));
				
				}
			if (e.getSource() == bezout) {
				
				ModuleCommun.calc_bezout(BigInteger.valueOf(Long.parseLong(eA.getText())), BigInteger.valueOf(Long.parseLong(eB.getText())));
				
				U.setText(String.valueOf(ModuleCommun.val_u));
				V.setText(String.valueOf(ModuleCommun.val_v));
				
			}
			if (e.getSource() == calc_m) {
				
				result_m = ModuleCommun.calcul_m(Integer.parseInt(nA.getText()),Integer.parseInt(cA.getText()), Integer.parseInt(cB.getText()));
				
				m.setText(String.valueOf(result_m));
				
			}
		}
		
		
		
		
	}
	

}

